package com.hms.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Hibernate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hms.dao.IBaseDao;
import com.hms.dao.ILocationDao;
import com.hms.entity.TdLocation;
import com.hms.service.ILocationService;

@Service("locationService")
@Transactional
public class LocationService extends BaseService<TdLocation, String> implements
		ILocationService {

	@Resource(name = "locationDao")
	private ILocationDao locationDao;

	@Override
	public IBaseDao<TdLocation, String> getDao() {
		return locationDao;
	}

	@Override
	public List<TdLocation> getRootLocationList() {
		List<TdLocation> rootLoction = locationDao.getRootLocationList();
		initializeListBean(rootLoction);
		return rootLoction;
	}

	@Override
	public List<TdLocation> getParentLocationList(TdLocation childrenLocation) {
		List<TdLocation> parentLocation = locationDao
				.getParentLocationList(childrenLocation);
		initializeListBean(parentLocation);
		return parentLocation;
	}

	@Override
	public List<TdLocation> getAllChildrenLocationList(TdLocation parentLocation) {
		List<TdLocation> childrenList = locationDao
				.getAllChildrenLocationList(parentLocation);
		initializeListBean(childrenList);
		return childrenList;
	}

	@Override
	public List<TdLocation> getAllImportCityList() {
		List<TdLocation> importCity = locationDao.getAllImportCityList();
		initializeListBean(importCity);
		return importCity;
	}

	/**
	 * 手动强制加载
	 * @param listBean  需要加载的listBean
	 */
	private void initializeListBean(List<TdLocation> listBean) {
		if (null != listBean) {
			for (TdLocation location : listBean) {
				// 强制加载
				Hibernate.initialize(location);
			}
		}
	}
}
